package com.jt.www.model.param;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jt.www.exception.ParamException;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.util.StringUtils;


public class PageParam implements BaseParam {
    private static String DESC = " desc";
    private static String ASC = " asc";
    @ApiModelProperty(value = "请求第几页", required = true)
    private Integer pn = 1;
    @ApiModelProperty(value = "页面大小", required = true)
    private Integer ps = 10;
    @ApiModelProperty(value = "排序字段")
    private String orderby;
    @ApiModelProperty(value = "是否为倒序")
    private Boolean desc = true;

    public static Page orderHandle(PageParam pageParam) {
        if (!StringUtils.isEmpty(pageParam.getOrderby())) {
            PageHelper.orderBy(pageParam.getOrderby() + (pageParam.getDesc() ? DESC : ASC));
        }
        return PageHelper.startPage(pageParam.getPn(), pageParam.getPs());
    }

    public static Page orderHandler(PageParam pageParam) {

        if (!StringUtils.isEmpty(pageParam.getOrderby())) {
            PageHelper.orderBy(pageParam.getOrderby() + (pageParam.getDesc() ? DESC : ASC));
        }
        return PageHelper.startPage(pageParam.getPn(), pageParam.getPs());
    }

    public static String getDESC() {
        return DESC;
    }

    public static void setDESC(String DESC) {
        PageParam.DESC = DESC;
    }

    public static String getASC() {
        return ASC;
    }

    public static void setASC(String ASC) {
        PageParam.ASC = ASC;
    }

    public Page orderHandle() {
        if (!StringUtils.isEmpty(this.getOrderby())) {
            PageHelper.orderBy(this.getOrderby() + (this.getDesc() ? DESC : ASC));
        }
        return PageHelper.startPage(this.getPn(), this.getPs());
    }

    @Override
    public void checkParam() throws ParamException {
        if (getPn() == null) {
            throw new ParamException("请求页数不能为空");
        }
        if (getPn() < 1) {
            throw new ParamException("请求页数不能小于1");
        }
        if (getPs() < 1) {
            throw new ParamException("页面大小不能小于1");
        }
    }

    public Integer getPn() {
        return pn;
    }

    public void setPn(Integer pn) {
        this.pn = pn;
    }

    public Integer getPs() {
        return ps;
    }

    public void setPs(Integer ps) {
        this.ps = ps;
    }

    public String getOrderby() {
        return orderby;
    }

    public void setOrderby(String orderby) {
        this.orderby = orderby;
    }

    public Boolean getDesc() {
        return desc;
    }

    public void setDesc(Boolean desc) {
        this.desc = desc;
    }
}
